package com.steven.forum.dao

import com.steven.forum.pojo.Discuss
import com.steven.forum.pojo.ForumInfo
import com.steven.forum.pojo.UserForumHistory
import com.steven.forum.pojo.UserIcon
import org.apache.ibatis.annotations.Insert
import org.apache.ibatis.annotations.Select
import org.apache.ibatis.annotations.Update
import org.springframework.stereotype.Repository

@Repository
interface ForumDao {
    @Select("SELECT\n" +
            "\tuname,\n" +
            "\ticonUrl,\n" +
            "\tresultB.fid,\n" +
            "\tresultB.uid,\n" +
            "\tresultB.title,\n" +
            "\tresultB.content,\n" +
            "\tresultB.images,\n" +
            "\tresultB.readNum,\n" +
            "\tresultB.createTime,\n" +
            "\tresultB.discussNum \n" +
            "FROM\n" +
            "\t(\n" +
            "\tSELECT\n" +
            "\t\tresultA.fid,\n" +
            "\t\tresultA.uid,\n" +
            "\t\tresultA.title,\n" +
            "\t\tresultA.content,\n" +
            "\t\tresultA.images,\n" +
            "\t\tresultA.readNum,\n" +
            "\t\tresultA.createTime,\n" +
            "\t\tcount( forum_discuss_info.fid ) discussNum \n" +
            "\tFROM\n" +
            "\t\t(\n" +
            "\t\tSELECT\n" +
            "\t\t\tforum_info.fid,\n" +
            "\t\t\tforum_info.uid,\n" +
            "\t\t\tforum_info.title,\n" +
            "\t\t\tforum_info.content,\n" +
            "\t\t\tforum_info.images,\n" +
            "\t\t\tcount( forum_user_history.fid ) readNum,\n" +
            "\t\t\tforum_info.createTime \n" +
            "\t\tFROM\n" +
            "\t\t\tforum_info\n" +
            "\t\t\tLEFT JOIN forum_user_history ON forum_info.fid = forum_user_history.fid \n" +
            "\t\t\twhere forum_info.createTime>#{startTime} \n" +
            "\t\tGROUP BY\n" +
            "\t\t\tforum_info.fid \n" +
            "\t\tHAVING\n" +
            "\t\t\treadNum >= 0 \n" +
            "\t\t) resultA\n" +
            "\t\tLEFT JOIN forum_discuss_info ON resultA.fid = forum_discuss_info.fid \n" +
            "\tGROUP BY\n" +
            "\t\tresultA.fid \n" +
            "\tHAVING\n" +
            "\t\tdiscussNum >= 0 \n" +
            "\t) resultB\n" +
            "\tLEFT JOIN user_detail ON resultB.uid = user_detail.uid \n" +
            "ORDER BY\n" +
            "\tdiscussNum DESC,\n" +
            "\tresultB.readNum DESC,\n" +
            "\tresultB.createTime DESC \n" +
            "\tLIMIT 0,#{pageEnd}")
    fun queryHotForumList(uid: Long?, pageEnd: Long, startTime: Long = System.currentTimeMillis() - 7 * 24 * 60 * 60 * 1000): List<ForumInfo>?

    @Select("SELECT\n" +
            "\tuname,\n" +
            "\ticonUrl,\n" +
            "\tresultB.fid,\n" +
            "\tresultB.uid,\n" +
            "\tresultB.title,\n" +
            "\tresultB.content,\n" +
            "\tresultB.images,\n" +
            "\tresultB.readNum,\n" +
            "\tresultB.createTime,\n" +
            "\tresultB.discussNum \n" +
            "FROM\n" +
            "\t(\n" +
            "\tSELECT\n" +
            "\t\tresultA.fid,\n" +
            "\t\tresultA.uid,\n" +
            "\t\tresultA.title,\n" +
            "\t\tresultA.content,\n" +
            "\t\tresultA.images,\n" +
            "\t\tresultA.readNum,\n" +
            "\t\tresultA.createTime,\n" +
            "\t\tcount( forum_discuss_info.fid ) discussNum \n" +
            "\tFROM\n" +
            "\t\t(\n" +
            "\t\tSELECT\n" +
            "\t\t\tforum_info.fid,\n" +
            "\t\t\tforum_info.uid,\n" +
            "\t\t\tforum_info.title,\n" +
            "\t\t\tforum_info.content,\n" +
            "\t\t\tforum_info.images,\n" +
            "\t\t\tcount( forum_user_history.fid ) readNum,\n" +
            "\t\t\tforum_info.createTime \n" +
            "\t\tFROM\n" +
            "\t\t\tforum_info\n" +
            "\t\t\tLEFT JOIN forum_user_history ON forum_info.fid = forum_user_history.fid \n" +
            "\t\tGROUP BY\n" +
            "\t\t\tforum_info.fid \n" +
            "\t\tHAVING\n" +
            "\t\t\treadNum >= 0 \n" +
            "\t\t) resultA\n" +
            "\t\tLEFT JOIN forum_discuss_info ON resultA.fid = forum_discuss_info.fid \n" +
            "\tGROUP BY\n" +
            "\t\tresultA.fid \n" +
            "\tHAVING\n" +
            "\t\tdiscussNum >= 0 \n" +
            "\t) resultB\n" +
            "\tLEFT JOIN user_detail ON resultB.uid = user_detail.uid \n" +
            "ORDER BY\n" +
            "\tresultB.createTime DESC,\n" +
            "\tdiscussNum DESC,\n" +
            "\tresultB.readNum DESC \n" +
            "\tLIMIT 0,#{pageEnd}")
    fun queryNewForumList(uid: Long?, pageEnd: Long): List<ForumInfo>?

    @Select("SELECT\n" +
            "\tfid,\n" +
            "\tforum_info.uid,\n" +
            "\tuname,\n" +
            "\ttitle,\n" +
            "\tcontent,\n" +
            "\timages,\n" +
            "\ticonUrl,\n" +
            "\tcreateTime \n" +
            "FROM\n" +
            "\tforum_info\n" +
            "\tLEFT JOIN user_detail ON forum_info.uid = user_detail.uid \n" +
            "WHERE\n" +
            "\tfid=#{fid}")
    fun queryForumByFid(fid: Long): List<ForumInfo>?

    @Insert("INSERT INTO forum_info(uid,title,content,images,createTime) VALUE(#{uid}, #{title},#{content},#{images},#{createTime})")
    fun addForum(uid: Long, title: String, content: String, images: String, createTime: Long = System.currentTimeMillis()): Int

    @Select("select * from forum_user_history where uid=#{uid} and fid=#{forumId}")
    fun getUserHistoryByForumId(uid: Long, forumId: Long): List<UserForumHistory>?

    @Insert("insert ignore into forum_user_history(uid,fid,createTime) value(#{uid},#{forumId},#{createTime})")
    fun addUserHistory(uid: Long, forumId: Long, createTime: Long = System.currentTimeMillis()): Int

    @Update("update forum_user_history set createTime=#{createTime} where uid=#{uid} and fid=#{forumId}")
    fun updateUserHistory(uid: Long, forumId: Long, createTime: Long = System.currentTimeMillis()): Int

    @Select("SELECT\n" +
            "\tdiscussId,uname,iconUrl,forum_discuss_info.uid,fid,content,createTime\n" +
            "FROM\n" +
            "\tforum_discuss_info \n" +
            "\tLEFT JOIN user_detail ON forum_discuss_info.uid = user_detail.uid\n" +
            "WHERE fid=#{fid} ORDER BY createTime asc")
    fun getDiscuss(fid: Long): List<Discuss>?

    @Insert("INSERT INTO forum_discuss_info(fid,uid,content,createTime) VALUE(#{fid},#{uid},#{content},#{createTime})")
    fun addDiscuss(uid: Long, fid: Long, content: String, createTime: Long = System.currentTimeMillis()): Int

    @Select("SELECT\n" +
            "\ticonUrl realIconUrl from forum_info\n" +
            "\tLEFT JOIN user_detail ON forum_info.uid = user_detail.uid \n" +
            "WHERE\n" +
            "\tfid=#{fid}")
    fun queryUserIconUrlByFid(fid: Long): List<UserIcon>
}